package com.hospital.data.simulation.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hospital.common.dto.data.simulation.InspectionReportDto;
import com.hospital.common.pojo.TDiagnosis;
import com.hospital.common.pojo.TUser;
import com.hospital.common.vo.data.simulation.InspectionReportVo;
import com.hospital.data.simulation.service.InspectionReportService;
import com.hospital.data.simulation.service.TDiagnosisService;
import com.hospital.data.simulation.service.TUserService;
import com.hospital.data.simulation.utils.InspectionGenerator;
import com.hospital.data.simulation.utils.MinioUtils;
import com.hospital.data.simulation.utils.PDFGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.hospital.common.base.interfaces.Const.CONTENT_TYPE_PDF;
import static com.hospital.common.base.interfaces.Const.PDF_SUFFIX;

@Service
public class InspectionReportServiceImpl implements InspectionReportService {
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private PDFGenerator pdfGenerator;
    @Autowired
    private TDiagnosisService tDiagnosisService;
    @Autowired
    private TUserService tUserService;
    @Override
    public List<InspectionReportVo> query(InspectionReportDto inspectionReportDto) {
        LambdaQueryWrapper<TDiagnosis> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TDiagnosis::getPatientId,inspectionReportDto.getPatientId());
        List<TDiagnosis> diagnosisList = tDiagnosisService.list(queryWrapper);
        return generator(preGenerator(diagnosisList),diagnosisList);
    }
    private List<ByteArrayOutputStream> preGenerator(List<TDiagnosis> diagnosisList){
        List<ByteArrayOutputStream> baosList = new ArrayList<>();
        if(!ObjectUtils.isEmpty(diagnosisList)){
            for(TDiagnosis diagnosis : diagnosisList){
                LambdaQueryWrapper<TUser> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(TUser::getId,diagnosis.getPatientId());
                TUser patient = tUserService.getOne(queryWrapper);
                try {
                    String bodyText = "检查人：" + patient.getName() + "\n" +
                            "检查代码：" + InspectionGenerator.generate() + "\n" +
                            "结果时间：" + LocalDateTime.now() + "\n";
                    baosList.add(pdfGenerator.generateFormattedPdf(bodyText));
                }catch (Exception ignore){
                }
            }
        }else{
            try {
                baosList.add(pdfGenerator.generateFormattedPdf("无检查结果"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return baosList;
    }
    private List<InspectionReportVo> generator(List<ByteArrayOutputStream> baosList,List<TDiagnosis> diagnosisList){
        List<InspectionReportVo> inspectionReportVoList = new ArrayList<>();
        Long i = 0L;
        int index = 0;
        for (ByteArrayOutputStream baos : baosList){
            String objectName = "ISC" + Arrays.toString(UUID.randomUUID().toString().split("-"))+PDF_SUFFIX;
            String pdfUrl = minioUtils.getPresignedUrl(minioUtils.uploadFile(objectName,new ByteArrayInputStream(baos.toByteArray()),baos.size(),CONTENT_TYPE_PDF));
            if(!ObjectUtils.isEmpty(diagnosisList)){
                inspectionReportVoList.add(new InspectionReportVo(i, diagnosisList.get(index).getInspectionName(), LocalDateTime.now(),pdfUrl));
                index++;
            }else{
                inspectionReportVoList.add(new InspectionReportVo(i, "无检查结果", LocalDateTime.now(),pdfUrl));
            }
        }
        return inspectionReportVoList;
    }
}
